package com.sam

import com.sam.MarkSix.Companion.count
import java.util.Comparator
import kotlin.random.Random
import kotlin.time.toDuration

class MarkSix {
    companion object{
        const val count = 49 // 几个数
        const val earn = 40 // 赢的倍数
    }


    fun Double.to2String(): String {
        return "%.2f".format(this)
    }

    fun Int.toNormal():String { //index 变成自然数
        return (this + 1).toString()
    }

    val a = IntArray(count)

    fun print() {
        for ((i, item) in a.withIndex()) {
            if (item > 0) {
                println("${i.toNormal()} 买了：$item")
            }
        }
        println("共买了：${a.sum()}")
    }

    // 全吃的最好及最坏情况，及平均概率
    fun eatAll() {
        val sum = a.sum()
        val eatEarn = IntArray(count)
        val map = HashMap<Int, MutableList<Int>>() // 盈亏值 - 对应的各个项

        for ((i, item) in a.withIndex()) {
            eatEarn[i] = sum - item * earn
            if (map.containsKey(eatEarn[i])) {
                map[eatEarn[i]]?.add(i)
            } else {
                map[eatEarn[i]] = ArrayList()
                map[eatEarn[i]]?.add(i)
            }
        }

        println("全吃：")
        println("平均收益：${a.average().to2String()}")
        map.toSortedMap(Comparator { o1, o2 ->
            if (o1 == o2) {
                0
            } else if ((map[o1]?.size ?: 0) == (map[o2]?.size ?: 0)) {
                o2 - o1 // 概率相同则收益 高的放前面
            } else {//概率高的放前面
                (map[o2]?.size ?: 0) - (map[o1]?.size ?: 0)
            }
        })
        .forEach { (t, u) ->
            println("收益：$t, 概率:${(u.size*100.0 / count).to2String()}%, 开：${u.joinToString(transform = { it.toNormal()})}")
        }
    }


    // 盈利最大策略
    fun best() {
        val eat = IntArray(count) // 吃的项

    }


}



fun main() {
    val markSix = MarkSix()
    val a = markSix.a
    val r = Random(System.currentTimeMillis())
    // 测试，随机买两注
    a[0] = r.nextInt(100)
    a[1] = r.nextInt(100)
    a[2] = r.nextInt(100)

    markSix.print()
    print("---------------")
    markSix.eatAll()



    val eat = IntArray(count)
}



